package com.mrjy.coursepackage.bo;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.mrjy.common.entity.CoursePackageCommentInfo;
import com.mrjy.common.entity.CoursePackageInfo;
import com.mrjy.common.entity.IntegralInfo;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.util.DateUtil;
import com.mrjy.common.util.ExcelUtil;
import com.mrjy.common.util.GlobalConstant;
import com.mrjy.common.util.MessyCodeUtil;
import com.mrjy.coursepackage.dao.ICoursePackageCommentInfoDao;
import com.mrjy.coursepackage.dao.ICoursePackageInfoDao;
import com.mrjy.coursepackage.dto.CoursePackageCommentInfoDTO;
import com.mrjy.coursepackage.dto.CoursePackageInfoDTO;
import com.mrjy.integral.util.IntegralEnum;
import com.mrjy.integral.util.IntegralUtil;
import com.mrjy.maintain.bo.IUserMemberInfoBo;

/**
 * 课程包和视频关联关系 业务实现类
 *
 * @author csshui
 */
@Service("coursePackageCommentInfoBo")
public class CoursePackageCommentInfoBo implements ICoursePackageCommentInfoBo {

    @Resource
    ICoursePackageCommentInfoDao coursePackageCommentInfoDao;
    @Resource
    private ICoursePackageInfoDao coursePackageInfoDao;
    
    @Resource
    private IUserMemberInfoBo userMemberInfoBo;

    @Override
    public Object txInsert(CoursePackageCommentInfo info) {
        Map<String, Object> map = new HashMap<String, Object>();
        int count = coursePackageCommentInfoDao.insert(info);
        if (count > 0) {
            map.put("err_code", 0);
            map.put("err_msg", "插入成功");
        } else {
            map.put("err_code", 1);
            map.put("err_msg", "插入失败");
        }
        return map;
    }

    @Override
    public Object txUpdate(CoursePackageCommentInfo info) {
        Map<String, Object> map = new HashMap<String, Object>();
        int count = coursePackageCommentInfoDao.update(info);
        if (count > 0) {
            map.put("err_code", 0);
            map.put("err_msg", "更新成功");
        } else {
            map.put("err_code", 1);
            map.put("err_msg", "更新失败");
        }
        return map;
    }

    @Override
    public Object txUpdateById(CoursePackageCommentInfo info) {
        Map<String, Object> map = new HashMap<String, Object>();
        info.setUpdateDate(new Timestamp(System.currentTimeMillis()));
        int count = coursePackageCommentInfoDao.updateById(info);
        if (count > 0) {
            map.put("err_code", 0);
            map.put("err_msg", "更新成功");
        } else {
            map.put("err_code", 1);
            map.put("err_msg", "更新失败");
        }
        return map;
    }

    @Override
    public Object txDelete(Integer id) {
        Map<String, Object> map = new HashMap<String, Object>();
        int count = coursePackageCommentInfoDao.delete(id);
        if (count > 0) {
            map.put("err_code", 0);
            map.put("err_msg", "删除成功");
        } else {
            map.put("err_code", 1);
            map.put("err_msg", "删除失败");
        }
        return map;
    }

    @Override
    public Object selectById(Integer id) {
        Map<String, Object> map = new HashMap<String, Object>();
        CoursePackageCommentInfo info = coursePackageCommentInfoDao.selectById(id);
        if (null != info) {
            map.put("err_code", 0);
            map.put("err_msg", "查询成功");
            map.put("info", info);
        } else {
            map.put("err_code", 1);
            map.put("err_msg", "查询失败");
            map.put("info", info);
        }
        return map;
    }

    @Override
    public Object insertSelective(CoursePackageCommentInfo info) {
        Map<String, Object> map = new HashMap<String, Object>();
        info.setUpdateDate(new Timestamp(System.currentTimeMillis()));
        info.setDisableFlag(0);
        int count = coursePackageCommentInfoDao.insertSelective(info);

        if (count > 0) {
            //添加积分
            IntegralInfo integral = IntegralUtil.setIntegral(info.getUserId()
                    , IntegralEnum.COURSE_COMMENT.getScore()
                    , IntegralEnum.COURSE_COMMENT.getTypeDesc()
                    , IntegralEnum.COURSE_COMMENT.getTypeId()
                    , info.getId());

            map.put("err_code", 0);
            map.put("score", integral.getScore());
            map.put("err_msg", "插入成功");
        } else {
            map.put("err_code", 1);
            map.put("err_msg", "插入失败");
        }
        return map;
    }

    @Override
    public Object updateDisableFlag(CoursePackageCommentInfo info) {
        Map<String, Object> map = new HashMap<String, Object>();
        int count = coursePackageCommentInfoDao.updateDisableFlag(info);
        if (count > 0) {
            map.put("err_code", 0);
            map.put("err_msg", "更新成功");
        } else {
            map.put("err_code", 1);
            map.put("err_msg", "更新失败");
        }
        return map;
    }

    @Override
    public Object selectUserDetailByCId(CoursePackageCommentInfo info, HttpSession session) {
        Map<String, Object> map = new HashMap<String, Object>();
        UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
        if (null == loginUser) {
            map.put("err_code", -1);
            map.put("err_msg", "用户未登录");
            return map;
        }
        info.setUserId(loginUser.getId());
        CoursePackageCommentInfo coursePackageCommentInfo = coursePackageCommentInfoDao.selectUserDetailByCId(info);
        if (null != coursePackageCommentInfo) {
            map.put("err_code", 0);
            map.put("err_msg", "查询成功");
            map.put("info", coursePackageCommentInfo);
        } else {
            map.put("err_code", 1);
            map.put("err_msg", "没有数据");
            map.put("info", coursePackageCommentInfo);
        }
        return map;
    }

    @Override
    public Object selectPageByCid(CoursePackageCommentInfo info) {
        Map<String, Object> map = new HashMap<String, Object>();
        int total = coursePackageCommentInfoDao.countSelectPageByCid(info);
        CoursePackageInfo packageInfo = new CoursePackageInfo();
        packageInfo.setId(info.getCoursePackageId());
        CoursePackageInfoDTO dto = coursePackageInfoDao.selectTeacherPackageByIdAndType(packageInfo);
        List<CoursePackageCommentInfoDTO> list = coursePackageCommentInfoDao.selectPageByCid(info);
        if (null != list && list.size() > 0) {
            map.put("err_code", 0);
            map.put("err_msg", "查询成功");
            map.put("rows", list);
            map.put("total", total);
            if (total >= 5) {
                map.put("average_start", dto.getAverageStart());
            } else {
                map.put("average_start", new BigDecimal(9.5));
            }
        } else {
            map.put("err_code", 1);
            map.put("err_msg", "没有数据");
            map.put("rows", list);
            map.put("total", total);
            map.put("average_start", new BigDecimal(9.5));
        }
        return map;
    }

    @Override
    public Object selectPageList(CoursePackageCommentInfo info) {
        Map<String, Object> map = new HashMap<String, Object>();
        int total = coursePackageCommentInfoDao.countSelectPageList(info);
        CoursePackageInfo packageInfo = new CoursePackageInfo();
        packageInfo.setId(info.getCoursePackageId());
        List<CoursePackageCommentInfo> list = coursePackageCommentInfoDao.selectPageList(info);
        if (null != list && list.size() > 0) {
            map.put("err_code", 0);
            map.put("err_msg", "查询成功");
            map.put("rows", list);
            map.put("total", total);
        } else {
            map.put("err_code", 1);
            map.put("err_msg", "没有数据");
            map.put("rows", list);
            map.put("total", total);
        }
        return map;
    }

    @Override
    public Object export(CoursePackageCommentInfo info, HttpServletResponse response, HttpSession session) {
        Map<String, Object> map = new HashMap<String, Object>();
        UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
        if (null == loginUser) {
            map.put("err_code", -1);
            map.put("err_msg", "用户未登录");
            return map;
        }
        info.setUserId(null == loginUser ? null : loginUser.getId());

        List<CoursePackageCommentInfo> dataset = coursePackageCommentInfoDao.selectPageList(info);
        if (dataset == null || dataset.size() == 0) {
            map.put("err_code", 404);
            map.put("err_msg", "无可导出数据");
            return map;
        }
        try {
            ExcelUtil.writeExcel(response, "评价" + DateUtil.dateToString(new Date(), DateUtil.FULL_DATE_STR) + ".xls", dataset, CoursePackageCommentInfo.class);
            map.put("err_code", 200);
            map.put("err_msg", "导出成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("err_code", 500);
            map.put("err_msg", "导出失败");
        }
        return map;
    }
}
